package com.zmcn.msg.business.msgGroup.service.impl;

import java.io.File;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zmcn.msg.bean.po.Msg;
import com.zmcn.msg.bean.po.MsgGroup;
import com.zmcn.msg.bean.vo.MsgGroupVO;
import com.zmcn.msg.bean.vo.MsgVO;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.business.msgGroup.dao.MsgGroupDao;
import com.zmcn.msg.business.msgGroup.service.MsgGroupService;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.util.FileOP;

/**
 * 
 * 上传消息批次信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class MsgGroupServiceImpl implements MsgGroupService {
	private MsgGroupDao dao;

	@Autowired
	public void setDao(MsgGroupDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取上传消息批次信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<MsgGroupVO> getList(Page<MsgGroupVO> page) {
		return dao.getList(page);
	}

	/**
	 * 根据ID获取上传消息批次信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public Page<MsgVO> getMsgGroupVO(Page<MsgVO> page,String batchId) {
		return dao.getMsgGroupVO(page,batchId);
	}

	/**
	 * 导入
	 * @param fileName
	 * @param file
	 * @param user
	 * @param userVO
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean imp(String fileName, File file,String user,UserVO userVO ) {
		boolean result = false;  
		String c = FileOP.readFile(file, "gb2312");
		String[] line = c.split("\r\n");
		
		MsgGroup msgGroup = new MsgGroup();
		msgGroup.setStatus(0);
		msgGroup.setUploadNum(line.length);
		msgGroup.setUploadTime(new Date());
		msgGroup.setReceiver(user);
		msgGroup.setUploador(userVO.getAccount());
		dao.create(msgGroup);
		
		for(int i=0;i<line.length;i++) {
			Msg msg = new Msg();
			msg.setBatchId(msgGroup.getBatchId());
			msg.setContent(line[i]);
			dao.create(msg);
		}
		result = true;
		return result;
	}	

}
